package mvp.levelup.threads;

import com.sun.deploy.uitoolkit.ui.ConsoleController;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.*;

/**
 * Created by Valera on 18.05.2014.
 */
public class Loggers {
    public static Logger threadLogger;

    static {
        automaticSetup();

//        threadLogger = customSetup();
    }

    private static void automaticSetup() {
        Properties preferences = new Properties();
        try {
            FileInputStream configFile = new FileInputStream("./log.properties");
            preferences.load(configFile);
            LogManager.getLogManager().readConfiguration(configFile);
        } catch (IOException ex) {
            System.err.println("WARNING: Could not open configuration file");
            System.err.println("WARNING: Logging not configured (console output only)");
        }
    }

    private static Logger customSetup() {
        Logger threadLogger = Logger.getLogger("threadLogger");
        try {
            threadLogger = Logger.getLogger("threadLogger");
            threadLogger.setLevel(Level.WARNING);
            Handler[] handlers = threadLogger.getHandlers();
            for (int i = 0; i < handlers.length; i++) {
                threadLogger.removeHandler(handlers[i]);
            }
            ConsoleHandler consoleHandler = new ConsoleHandler();
            consoleHandler.setLevel(Level.INFO);
            threadLogger.addHandler(consoleHandler);
            FileHandler fileHandler = new FileHandler("mylogger.log", 10 * 1024, 2, true);
            fileHandler.setLevel(Level.WARNING);
            threadLogger.addHandler(fileHandler);
        } catch (Exception e) {
            threadLogger.warning(e.getMessage());
        }
        return threadLogger;
    }
}
